<script setup lang="ts">
import { ref, reactive } from "vue";
import { addModel, updateModel } from "@/api/model";
import SingleUpload from "@/components/upload/SingleUpload.vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";

const dialogVisible = ref(false);

interface ModelForm {
  modelName: string;
  modelDesc: string;
  modelPic: string;
  modelVersion: string;
  modelAuthor: string;
  modelCall: string;
  modelUrl: string;
  status: number;
}

const ruleFormRef = ref<FormInstance>();

const title = ref("新增");

let form = reactive<ModelForm>({
  id: "",
  modelName: "",
  modelDesc: "",
  modelPic: "",
  modelVersion: "",
  modelAuthor: "",
  modelCall: "",
  modelUrl: "",
  status: 1,
});

const rules = reactive<FormRules<ModelForm>>({
  modelName: [
    {
      required: true,
      message: "请输入模型名称",
      trigger: "blur",
    },
  ],
  modelAuthor: [
    {
      required: true,
      message: "请输入模型作者",
      trigger: "blur",
    },
  ],
  modelDesc: [
    {
      required: true,
      message: "请输入模型作者",
      trigger: "blur",
    },
  ],
  modelPic: [
    {
      required: true,
      message: "请输入模型作者",
      trigger: "change",
    },
  ],
  modelCall: [
    {
      required: true,
      message: "请输入模型调用方式",
      trigger: "blur",
    },
  ],
  modelUrl: [
    {
      required: true,
      message: "请输入模型下载地址",
      trigger: "blur",
    },
  ],
});

const open = (row: any) => {
  console.log("row", row);
  if (row) {
    title.value = "修改";
    form = row;
  } else {
    title.value = "新增";
  }

  dialogVisible.value = true;
};

const close = () => {
  dialogVisible.value = false;
};

const emit = defineEmits(["on-result"]);

const submit = () => {
  console.log(form);

  ruleFormRef.value.validate((valid) => {
    if (valid) {
      if (!form.id) {
        addModel(form).then((res) => {
          if (res.code == 200) {
            ElMessage.success("新增成功");
            emit("on-result", form);
            close();
          } else {
            ElMessage.error(res.msg);
          }
        });
      } else {
        updateModel(form).then((res) => {
          if (res.code == 200) {
            ElMessage.success("修改成功");
            emit("on-result", form);
            close();
          } else {
            ElMessage.error(res.msg);
          }
        });
      }
    } else {
      return false;
    }
  });
};

// 表单校验

defineExpose({
  open,
  close,
});
</script>

<template>
  <el-dialog v-if="dialogVisible" v-model="dialogVisible" :title="title" width="500px">
    <el-form ref="ruleFormRef" label-position="left" label-width="100px" :model="form" :rules="rules"
      style="max-width: 460px">
      <el-form-item label="模型名称" prop="modelName">
        <el-input v-model="form.modelName" maxlength="30" />
      </el-form-item>
      <el-form-item label="模型图片" prop="modelPic">
        <SingleUpload v-model="form.modelPic" />
      </el-form-item>
      <el-form-item label="模型描述" prop="modelDesc">
        <el-input type="textarea" v-model="form.modelDesc" />
      </el-form-item>
      <el-form-item label="模型版本" prop="modelVersion">
        <el-input v-model="form.modelVersion" />
      </el-form-item>

      <el-form-item label="模型作者" prop="modelAuthor">
        <el-input v-model="form.modelAuthor" />
      </el-form-item>

      <el-form-item label="模型调用" prop="modelCall">
        <el-select v-model="form.modelCall" placeholder="请选择">
          <el-option label="ALL" value="ALL" />
          <el-option label="GRPC" value="GRPC" />
          <el-option label="HTTP" value="HTTP" />
        </el-select>
      </el-form-item>

      <el-form-item label="模型URL" prop="modelUrl">
        <el-input v-model="form.modelUrl" />
      </el-form-item>

      <el-form-item label="模型状态" prop="modelStatus">
        <el-radio-group v-model="form.status">
          <el-radio-button :label="1">上架</el-radio-button>
          <el-radio-button :label="0">下架</el-radio-button>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">关闭</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
